
/**
 * Module dependencies
 */

var express = require('express'),
  bodyParser = require('body-parser'),
  methodOverride = require('method-override'),
  http = require('http'),
  path = require('path'),
  routes = require('./routes'),
  patient_api = require('./routes/patient_api')
  medecin_api = require('./routes/medecin_api')
;


var app = module.exports = express();

/**
 * Configuration
 */

// all environments
app.set('port', process.env.PORT || 80);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');


app.use(bodyParser.urlencoded({ extended: true,limit: '5mb'}));
app.use(bodyParser.json({limit: '5mb'}));
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));

/**
 * Routes
 */

// serve index and view partials
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);

// JSON API PATIENTS
app.post('/api/patient/all', patient_api.getAllPatients);
app.post('/api/patient/get', patient_api.getPatient);
app.post('/api/patient/update', patient_api.updatePatient);
app.post('/api/patient/delete', patient_api.deletePatient);


// JSON API MEDECINS
app.post('/api/medecin/all', medecin_api.getAllMedecins);
app.post('/api/medecin/get', medecin_api.getMedecin);
app.post('/api/medecin/update', medecin_api.updateMedecin);
app.post('/api/medecin/delete', medecin_api.deleteMedecin);


// redirect all others to the index (HTML5 history)
app.get('*', routes.index);


/**
 * Start Server
 */

http.createServer(app).listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
  patient_api.getAllPatients();
  medecin_api.getAllMedecins();
});
